package com.ktgj.flight.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ktgj.flight.domain.AirlinerOptionDTO;
import com.ktgj.flight.domain.FlightCountry;
import com.ktgj.flight.domain.vo.ChannelListVo;
import com.ktgj.flight.mapper.FlightCountryMapper;
import com.ktgj.flight.service.IFlightCountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.flight.mapper.AirlinerMapper;
import com.ktgj.flight.domain.Airliner;
import com.ktgj.flight.service.IAirlinerService;

/**
 * 航空公司Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-30
 */
@Service
public class AirlinerServiceImpl implements IAirlinerService 
{
    @Autowired
    private AirlinerMapper airlinerMapper;
    @Autowired
    private IFlightCountryService flightCountryService;
    @Autowired
    private FlightCountryMapper flightCountryMapper;

    /**
     * 查询航空公司
     * 
     * @param id 航空公司ID
     * @return 航空公司
     */
    @Override
    public Airliner selectAirlinerById(Long id)
    {
        Airliner airliner = airlinerMapper.selectAirlinerById(id);
        List<FlightCountry> flightCountries = flightCountryService.selectFlightCountryList(null);
        flightCountries.forEach(item -> {
            if (airliner.getCountryId().equals(item.getCountryId().stripTrailingZeros().toPlainString()))
                airliner.setCountryId(item.getCountryName());
        });
        return airliner;
    }

    /**
     * 查询航空公司列表
     * 
     * @param airliner 航空公司
     * @return 航空公司
     */
    @Override
    public List<Airliner> selectAirlinerList(Airliner airliner)
    {
        List<Airliner> airliners = airlinerMapper.selectAirlinerList(airliner);
//        List<FlightCountry> flightCountries = flightCountryService.selectFlightCountryList(null);
//        airliners.forEach(item -> {
//            flightCountries.forEach(items -> {
//                if (item.getCountryId().equals(items.getCountryId().stripTrailingZeros().toPlainString())) {
//                    item.setCountryId(items.getCountryName());
//                }
//            });
//        });
        if (airliners.size() > 0){
            for (Airliner TmpAirliner : airliners) {
                BigDecimal id = new BigDecimal(TmpAirliner.getCountryId() + ".000000000000000000000000000000");
                FlightCountry flightCountry = flightCountryMapper.flightCountryById(id);
                if (flightCountry != null){
                    TmpAirliner.setCountryName(flightCountry.getCountryName());
                }
            }
        }
        return airliners;
    }

    /**
     * 新增航空公司
     * 
     * @param airliner 航空公司
     * @return 结果
     */
    @Override
    public int insertAirliner(Airliner airliner)
    {
        return airlinerMapper.insertAirliner(airliner);
    }

    /**
     * 修改航空公司
     * 
     * @param airliner 航空公司
     * @return 结果
     */
    @Override
    public int updateAirliner(Airliner airliner)
    {
        return airlinerMapper.updateAirliner(airliner);
    }

    /**
     * 删除航空公司
     *
     * @param id 需要删除的航空公司ID
     * @return 结果
     */
    @Override
    public int deleteAirLinerById(Long id) {
        return airlinerMapper.deleteAirLinerById(id);
    }

    /**
     * 批量删除航空公司
     * 
     * @param ids 需要删除的航空公司ID
     * @return 结果
     */
    @Override
    public int deleteAirlinerByIds(Long[] ids)
    {
        return airlinerMapper.deleteAirlinerByIds(ids);
    }

    @Override
    public String queryAirlinerByAirlinerId(String airlinerId) {
        String airliner = airlinerMapper.queryAirlinerByAirlinerId(airlinerId);
        return airliner;
    }

    @Override
    public String queryAirliner(String airlinerId) {
        return airlinerMapper.queryAirliner(airlinerId);
    }

    @Override
    public List<AirlinerOptionDTO> queryAirlinerSelect() {

        return airlinerMapper.queryAirlinerSelect();
    }

    /**
     * 原始账单查询报表渠道列表
     * */
    @Override
    public List<ChannelListVo> channelList() {
        List<Airliner> airliners = airlinerMapper.channelList();
        if (airliners.size() > 0){
            List<ChannelListVo> channelListVoList = new ArrayList<>();
            for (Airliner airliner : airliners) {
                if (airliner.getAirlinerShorter() != null && !airliner.getAirlinerShorter().equals("") &&
                        airliner.getAirlinerId() != null && !airliner.getAirlinerId().equals("") &&
                        !airliner.getAirlinerShorter().equals("分隔符")){
                    ChannelListVo channelListVo = new ChannelListVo();
                    channelListVo.setId(airliner.getId());
                    channelListVo.setName(airliner.getAirlinerShorter() + airliner.getAirlinerId());
                    channelListVoList.add(channelListVo);
                }
            }
            return channelListVoList;
        }
        else {
            return null;
        }
    }

    /**
     * 代码获取航司名（远程）
     * */
    @Override
    public String airlinerName(String card) {
        Airliner airliner = airlinerMapper.selectName(card);
        String airlinerName = null;
        if (airliner != null && airliner.getAirliner() != null){
            int i = airliner.getAirliner().indexOf("航空");
            airlinerName = airliner.getAirliner().substring((i - 2), (i + 2));
        }
        return airlinerName;
    }

}
